﻿package util
{
	import flash.net.FileReference;
	
	/**
	 * ...
	 * @author gerry
	 */
	public class SoundUtil 
	{
		//public vars
		public static var soundLoaded:Boolean = false;
		
		//private vars
		private static var _info:String = "";
		
		//-------------------------------------------------------------------------
		//
		//public methods
		//
		//-------------------------------------------------------------------------
		public static function urlValid(url:String, types:Array):Boolean
		{
			if (url == null)
			{
				return false;
			}
			
			if (types == null)
			{
				return true;
			}
			
			var	fileType:String = SoundUtil.getFileType(url);
			if (fileType == null)
			{
				return false;
			}
			
			return types.indexOf(fileType)!=-1;			
		}
		
		public static function getFileType(obj:*):String
		{
			if (obj is FileReference)
			{
				return obj.extension;
			}			
			
			if (obj is String)
			{
				var commaIndex:int = obj.lastIndexOf(".");
				
				if (commaIndex == -1)
				{
					return null;
				}
				var	fileType:String = obj.slice(commaIndex+1).toLowerCase();
				
				return fileType;
			}
			
			return null;			
		}
		
		public static function addListener(target:*, type:String, listener:Function):void
		{
			if (target!=null && !target.hasEventListener(type))
			{
				target.addEventListener(type, listener);
			}
		}
		
		public static function removeListener(target:*, type:String):void
		{
			if (target!=null && target.hasEventListener(type))
			{
				target.removeEventListener(type);
			}
		}
		
		/**
		 * 
		 * @param	time milliseconds
		 * @return  String  hour:minute:second
		 */
		public static function formatTime(time:Number):String
		{
			var sec:Number = Math.floor(time / 1000 % 60);
			var min:Number = Math.floor(time / 60000);
			var hr:Number  = Math.floor(time / 3600000);
			
			return zeroFill(hr) + ':' + zeroFill(min) + ':' + zeroFill(sec);
		}
		
		private static function zeroFill(value:Number):String
		{
			return value < 10 ? "0" + value.toString() : value.toString();
		}

		public static function set info(value:String):void
		{
			if (SoundUtil._info != value)
			{
				SoundUtil._info = value;
			}
		}
		
		public static function get info():String
		{
			return SoundUtil._info;
		}
   }
}